//
//  AccountInfoController.m
//  weizhuan
//
//  Created by lixun on 13-8-2.
//  Copyright (c) 2013年 lixun. All rights reserved.
//

//#import "AccountController.h"
//#import "AppUtility.h"
//#import "NetUtility.h"
//#import "URLConfig.h"
//#import "UEXSqlite.h"
//#import "AppDelegate.h"
//#import "NSString+Alert.h"
//#import "UD.h"
//#import "MD5Digest.h"

int accountState;
NSDictionary *accountInfo;

@implementation AccountController

+ (BOOL)isLogin {
	return (accountState == kAccountStateLogin);
}

+ (BOOL)getAccountState {
	return accountState;
}

+ (void)setAccountState:(int)inState {
	accountState = inState;
}

+ (NSDictionary*)getAccountInfo {
	return accountInfo;
}

+ (void)setAccountInfo:(NSDictionary*)inUserInfo {
	[accountInfo release];
	accountInfo = nil;
	if (inUserInfo) {
		accountInfo = [inUserInfo retain];
	}
}

+ (BOOL)autoLogin:(NSDictionary*)inUserInfo {
  /*  NSString *name = [inUserInfo objectForKey:kPAccountName];
	NSString *password = [inUserInfo objectForKey:kPPassword];
    NSString *deviceTokenStr = [[NSUserDefaults standardUserDefaults] objectForKey:kUDDeviceTocken];
	NSString *macAddress = [NetUtility macAddress];
	NSString *versionStr = [[UIDevice currentDevice] systemVersion];
    
    NSMutableDictionary *postDict = [[NSMutableDictionary dictionaryWithCapacity:7] retain];
    [postDict setObject:name forKey:@"userEmail"];
    [postDict setObject:password forKey:@"userPwd"];
    [postDict setObject:deviceTokenStr forKey:@"devicetoken"];
    [postDict setObject:macAddress forKey:@"macaddress"];
    [postDict setObject:versionStr forKey:@"version"];
    [postDict setObject:@"0" forKey:@"platform"];
    [postDict setObject:@"apple" forKey:@"manufacturer"];
    
    UEXNSLog(@"postDict is %@", postDict);
	NSDictionary *dict = [NetUtility getDataWithURLStr:kURLLogin andPostDict:postDict];
    [postDict release];
	if ([dict isKindOfClass:[NSDictionary class]]) {
		if ([[dict objectForKey:@"status"] intValue] == 1) {
			NSDictionary *info = [dict objectForKey:@"body"];
			if ([info isKindOfClass:[NSDictionary class]]) {
                NSMutableDictionary *accountInfo = [NSMutableDictionary dictionaryWithDictionary:info];
                [accountInfo setObject:name forKey:kPAccountName];
                [accountInfo setObject:password forKey:kPPassword];
                [AccountController setAccountState:kAccountStateLogin];
                [AccountController setAccountInfo:accountInfo];
                return YES;
			}
		}
	}*/
	return NO;
}

+ (void)logout {
    [AccountController setAccountState:kAccountStateUnLogin];
    [AccountController setAccountInfo:nil];
   /* NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:[[AccountController getAccountInfo] objectForKey:kPUid],@"uid",nil];
	NSDictionary *dict = [NetUtility getDataWithURLStr:kURLLogout andPostDict:postDict andView:theApp.window];
	UEXNSLog(@"dict is %@", dict);
	if ([dict isKindOfClass:[NSDictionary class]]) {
		if ([[dict objectForKey:@"status"] intValue] == 1) {
			[self setAccountState:kAccountStateUnLogin];
			[self setAccountInfo:nil];
			[UEXSqlite deleteLoginTable];
            [UEXSqlite deleteDuiHuaDatabase];
            [UEXSqlite deleteNotificationDatabase];
            [UEXSqlite deleteBookBagDatabase];
            [UEXSqlite deleteShoppingCartDatabase];
            [UEXSqlite deleteChaptersDatabase];
            [AppUtility deleteBooks];
			[[NSUserDefaults standardUserDefaults] removeObjectForKey:kUDUserLocation];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUDNoteUnReadNum];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUDNotificationId];
			[[NSUserDefaults standardUserDefaults] synchronize];
			if ([UEXXMPP sharedInstance].isConnected && ![UEXXMPP sharedInstance].isLogout) {
				[[UEXXMPP sharedInstance] logOut];
			}
			[[UEXXMPP sharedInstance] clearInstance];
			[theApp performSelectorOnMainThread:@selector(backToStartView) withObject:nil waitUntilDone:NO];
		} else {
			NSString *error = [dict objectForKey:@"message"];
			if ([error isKindOfClass:[NSString class]]) {
				[error showAlert];
			}
		}
	}*/
}

@end
